package com.czk.durant.springframework.beans.factory.support;

import com.czk.durant.springframework.beans.factory.config.BeanDefinition;
import com.czk.durant.springframework.beans.BeanFactory;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/9 14:52
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String beanName) {
        return doGetBean(beanName, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        return doGetBean(beanName, args);
    }

    protected <T> T doGetBean(String beanName, Object[] args) {
        // 拿到单例
        Object singleton = getSingleton(beanName);
        if (singleton != null) {
            return (T) singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return (T) createBean(beanName, beanDefinition, args);

    }

    protected abstract BeanDefinition getBeanDefinition(String beanName);

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args);

}
